//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	SqlItem.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace Smart.Common.Entities
{
	/// <summary>
	/// SQL item 
	/// </summary>
	public class SqlItem
	{
		/// <summary>
		/// SQL item identifier
		/// </summary>
		public Identifier ID = Identifier.Empty;

		/// <summary>
		/// SQL item values
		/// </summary>
		public Dictionary<string, object> Values = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

		/// <summary>
		/// SQL item 
		/// </summary>
		public SqlItem()
		{
			if (!Values.ContainsKey("ID")) Values.Add("ID", ID);
			if (!Values.ContainsKey("Title")) Values.Add("Title", string.Empty);
		}

		/// <summary>
		/// SQL item 
		/// </summary>
		public SqlItem(Identifier id)
		{
			ID = id;
			if (!Values.ContainsKey("ID")) Values.Add("ID", ID);
			if (!Values.ContainsKey("Title")) Values.Add("Title", string.Empty);
		}

		/// <summary>
		/// Load columns map
		/// </summary>
		public static Dictionary<string, int> LoadMap(SqlDataReader reader)
		{
			var res = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

			for (int I = 0, ICnt = reader.FieldCount; I < ICnt; I++)
				res[reader.GetName(I)] = I;

			return res;
		}

		/// <summary>
		/// Load row values
		/// </summary>
		public void Load(SqlDataReader reader, Dictionary<string, int> map)
		{
			foreach (var column in map)
			{
				var value = reader[column.Value];

				Values[column.Key] = value == DBNull.Value ? null : value;
			}
		}

		/// <summary>
		/// Maps system ID field to specific primary key field name
		/// </summary>
		/// <param name="idField">primary key field name</param>
		public void MapSystemIDField(string idField)
		{
			if (!idField.IsNullOrEmpty() && !idField.EqIC("ID"))
			{
				Values["ID"] = Values[idField];
				Values.Remove(idField);
			}
		}

		/// <summary>
		/// Map system fields
		/// </summary>
		/// <param name="idField">primary key field name</param>
		/// <param name="titleField">title field name</param>
		public void MapSystemFields(string idField, string titleField)
		{
			if (!idField.IsNullOrEmpty() && !idField.EqIC("ID"))
			{
				Values["ID"] = Values[idField];
				Values.Remove(idField);
			}

			if (!titleField.IsNullOrEmpty() && !titleField.EqIC("Title"))
			{
				if (idField != titleField)
				{
					Values["Title"] = Values[titleField];
					Values.Remove(titleField);
				}
				else
				{
					Values["Title"] = Values["ID"];
				}
			}
		}

		/// <summary>
		/// Unmap system fields
		/// </summary>
		/// <param name="idField">primary key field name</param>
		/// <param name="titleField">title field name</param>
		public void UnmapSystemFields(string idField, string titleField)
		{
			if (!idField.IsNullOrEmpty() && !idField.EqIC("ID"))
			{
				Values[idField] = Values["ID"];
				Values.Remove("ID");
			}

			if (!titleField.IsNullOrEmpty() && !titleField.EqIC("Title"))
			{
				Values[titleField] = Values["Title"];
				Values.Remove("Title");
			}
		}
	}
}